<template>
  <div class="signup-page">
    <validate-form @form-submit="onSubmitForm">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input :rules="emailRules" v-model="formData.emailVal" placeholder="请输入邮箱地址"></validate-input>
      </div>
      <div class="mb-3">
        <label class="form-label">昵称</label>
        <validate-input :rules="nickNameRules" v-model="formData.nickNameVal" placeholder="请输入昵称"></validate-input>
      </div>
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input :rules="passwordRules" v-model="formData.passwordVal" placeholder="请输入密码" type="password"></validate-input>
      </div>
      <div class="mb-3">
        <label class="form-label">确认密码</label>
        <validate-input :rules="rePasswordRules" v-model="formData.rePasswordVal" placeholder="请再次输入密码" type="password"></validate-input>
      </div>
      <template #submit>
        <span class="btn btn-primary">注册</span>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
import { useRouter } from 'vue-router'
import ValidateInput, { RulesProp } from './../components/ValidateInput.vue'
import ValidateForm from './../components/ValidateForm.vue'
import axios from 'axios'
import CreateMessage from './../components/CreateMessage'
export default defineComponent({
  name: 'sign-up',
  components: {
    ValidateInput,
    ValidateForm
  },
  setup () {
    const router = useRouter()
    const formData = reactive({
      emailVal: '',
      nickNameVal: '',
      passwordVal: '',
      rePasswordVal: ''
    })
    const emailRules: RulesProp = [
      { type: 'required', message: '电子邮箱地址不能为空' },
      { type: 'email', message: '请输入正确的电子邮箱格式' }
    ]
    const nickNameRules: RulesProp = [
      { type: 'required', message: '昵称不能为空' }
    ]
    const passwordRules: RulesProp = [
      { type: 'required', message: '密码不能为空' },
      { type: 'minLength', message: '密码至少为6位数' },
      { type: 'maxLength', message: '密码最多不能超过20位数' }
    ]
    const rePasswordRules: RulesProp = [
      { type: 'required', message: '密码不能为空' },
      { type: 'minLength', message: '密码至少为6位数' },
      { type: 'maxLength', message: '密码最多不能超过20位数' },
      {
        type: 'custom',
        validator: () => {
          return formData.passwordVal === formData.rePasswordVal
        },
        message: '密码不相同'
      }
    ]
    const onSubmitForm = (result: boolean) => {
      if (result) {
        const playload = {
          email: formData.emailVal,
          nickName: formData.nickNameVal,
          password: formData.passwordVal
        }
        axios.post('/users', playload).then(() => {
          CreateMessage('注册成功，两秒后跳转到登录页面', 'success', 2000)
          setTimeout(() => {
            router.push({ name: 'login' })
          }, 2000)
        })
      }
    }
    return {
      formData,
      emailRules,
      nickNameRules,
      passwordRules,
      rePasswordRules,
      onSubmitForm
    }
  }
})
</script>
